; Initialization:
		org	0
		di
		im	1		; RST 38h mode
		xor	a		; LED off
		ld	i,a
		ld	d,80h		; D bit 7 on, rest off
		jp	1FFh

; WARNING: There's very little room to add anything to the initialization or
; to the Morse routine before the interrupt handler of mode 1.

; This is the output routine. It keeps outputting the bits of D endlessly.
; The ON time is long or short depending on the bit value, while the OFF time
; is short between bits, and long to distinguish that a new repetition is
; about to start (it always starts with a long OFF).
Morse:		ld	e,8		; 8 bits counter
		ld	b,210		; Long space, used as separator
Morse2:		xor	a		; LED off
		ld	i,a
Morse3:		dec	bc
		ld	a,b
		or	c
		jr	nz,Morse3	; Delay
		ld	a,4		; LED on
		ld	i,a
		ld	b,70		; Short flash
		rlc	d
		jr	nc,Morse4	; If current bit is 0, keep it Short
		ld	b,210		; Long flash
Morse4:		dec	bc
		ld	a,b
		or	c
		jr	nz,Morse4	; Delay
		ld	b,70		; Space between flashes is short
		dec	e		; Next bit
		jr	nz,Morse2	; Loop unless all 8 bits displayed
		jr	Morse		; Loop forever (sets space to long)

		org	38h
		; INT ISR
		set	6,d
		jr	Morse

RetPtr:		dw	CheckInt

CheckInt:	set	1,d		; Will INT happen here? Before? After?
		set	4,d		; Should not be reached unless buggy
		jr	Morse

		org	66h
		; NMI ISR
		set	3,d
		ld	sp,RetPtr	; There's no RAM, so point SP to ROM
		retn			; "Return" to CheckInt restoring IFF1

		org	1FFh
		inc	d		; Bit 0 on
		ei			; NMI triggers after this
		set	5,d		; Should not be reached
		jp	Morse		; Shouldn't be reached either

; Observed result: Bits set: 7, 6, 3, 1, 0, meaning:
; - Bits set, in order of execution:
;   - Bit 7: It executed the initialization
;   - Bit 0: It executed the instruction at 1FFh, right before asserting NMI
;   - Bit 3: It executed the NMI ISR
;   - Bit 1: It executed the 1st instruction of CheckInt
;   - Bit 6: It executed the INT routine (meaning EI at 200h was executed too)
; - Bits not set and their meaning:
;   - Lack of bit 5: It didn't execute the instruction after EI
;   - Lack of bit 4: It didn't execute the 2nd instruction of CheckInt
;   - Lack of bit 2: It's never set so it means nothing
